package com.lin.java.tool.send.config;

import com.lin.java.tool.send.config.properties.SmsProperties;
import com.lin.java.tool.send.email.CommonEmailStrategy;
import com.lin.java.tool.send.email.InitializeEmailTemplate;
import com.lin.java.tool.send.factory.EmailFactory;
import com.lin.java.tool.send.factory.SmsFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author linjiang
 * @date 2024/8/16
 * @fn
 */
@Configuration
public class SendConfig {
    @Autowired(required = false)
    private InitializeEmailTemplate initializeEmailTemplate;

    @Bean
    @ConditionalOnProperty(prefix = "sms", name = "platform")
    public SmsFactory smsFactory(SmsProperties smsProperties) {
        return new SmsFactory(smsProperties.getPlatform(), smsProperties.getParams());
    }

    @Bean
    @ConditionalOnProperty(prefix = "spring.mail", name = "host")
    public EmailFactory emailFactory(MailProperties mailProperties) {
        EmailFactory factory = new EmailFactory(mailProperties.getHost(), mailProperties.getPort(), mailProperties.getUsername(), mailProperties.getPassword(), mailProperties.getProtocol(), "UTF-8");
        factory.addStrategy(new CommonEmailStrategy(factory, initializeEmailTemplate));
        return factory;
    }

}
